﻿using System;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Security.Principal;

using SBPweb.Forge.Contracts.Components;
using SBPweb.Forge.Contracts.Logging;

using SBPweb.Forge.Modules.Communication;
using SBPweb.Forge.Modules.Modularity;
using SBPweb.Forge.Modules.Smartparts;

namespace SBPweb.PROvibe.Business.Server.Library.Modularity
{

    public class ModuleHost : InitializableBase, IModuleHost
    {
        
        #region Private fields

        private ModuleLoader moduleLoader;
        private ILogManager logger;
        private CultureInfo[] supportedCultures;

        #endregion

        #region Constructor logic

        public ModuleHost()
        {
            supportedCultures = new CultureInfo[] { Thread.CurrentThread.CurrentUICulture, Thread.CurrentThread.CurrentCulture };

            DirectoryInfo dI = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Modules\\");
            if (!dI.Exists)
            {
                Console.WriteLine("SBPweb PROvibe.NET ModuleHost Error: Module folder {0} was not found!", dI.Name);
                return;
            }
            AppDomain.CurrentDomain.AppendPrivatePath(dI.FullName);

            this.moduleLoader = new ModuleLoader(dI.FullName, "SBPweb.PROvibe.Modules.Server.*.dll", "SBPweb PROvibe.NET 3.0", "SBPweb tech", logger);
        }

        #endregion

        #region Property accessors

        public ModuleHostType HostType
        {
            get
            {
                return ModuleHostType.WebForms;
            }
        }

        public ILayoutProvider LayoutProvider
        {
            get { throw new NotImplementedException(); }
        }

        public ILogManager LogManager
        {
            get
            {
                return logger;
            }
            set
            {
                logger = value;
            }
        }

        public MenuBase Menu
        {
            get { throw new NotImplementedException(); }
        }

        public MessageQueueBase Messages
        {
            get { throw new NotImplementedException(); }
        }

        public ModuleLoader ModuleLoader
        {
            get
            {
                return moduleLoader;
            }
        }

        public IPrincipal SecurityPrincipal
        {
            get
            {
                return Thread.CurrentPrincipal;
            }
        }

        public SmartpartListBase Smartparts
        {
            get { throw new NotImplementedException(); }
        }

        public MenuBase StatusBar
        {
            get { throw new NotImplementedException(); }
        }

        public CultureInfo[] SupportedCultures
        {
            get
            {
                return supportedCultures;
            }
        }

        public IPersistable ParentPersistable
        {
            get { throw new NotImplementedException(); }
        }

        public string PersistencyKey
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region Events

        public event EventHandler ApplicationClosing;

        #endregion

        #region Methods

        protected override void OnInitialize()
        {
            this.moduleLoader.Initialize(this, "Default");
        }

        protected override void OnTerminate()
        {
            if (this.moduleLoader != null)
            {
                try
                {
                    this.moduleLoader.Terminate();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }

        public void SetCulture(CultureInfo ci)
        {
            throw new NotImplementedException();
        }

        #endregion

    }

}